package com.talent.service.impl;

import com.talent.entities.dto.Vacc;
import com.talent.entities.dto.VaccOrder;
import com.talent.mapper.PaymentMapper;
import com.talent.service.PaymentSericve;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.LinkedList;

/**
 * @author 周意朗
 * Date: 2022/4/26
 * @since JDK 11
 */
@Service
public class PaymentServiceImpl implements PaymentSericve {
    @Resource
    public PaymentMapper mapper;


    @Override
    public Vacc findVacc(String vaccid) {
        return mapper.findVacc(vaccid);
    }

    @Override
    public LinkedList<Vacc> selectFavorite(String tel) {
        String s = mapper.selectFavorite(tel);
        String[] split = s.split(";");
        LinkedList<Vacc> result = new LinkedList<>();
        for (String value : split) {
            result.add(mapper.findVacc(value));
        }
        return result;
    }

    @Override
    public String addFavorite(String userid, String vaccid) {
        String s = mapper.selectFavorite(userid);
        if (s == null || s.length() == 0) {
            s = vaccid + ";";
            if (mapper.addFavorite(userid, s)) {
                return "success";
            } else {
                return "false";
            }
        }
        String[] split = s.split(";");
        for (String value : split) {
            if (value.equals(vaccid)) {
                return "已经收藏了";
            }
        }
        StringBuilder stringBuilder = new StringBuilder(s);
        stringBuilder.append(vaccid).append(";");
        Boolean flag = mapper.addFavorite(userid, stringBuilder.toString());
        if (flag) {
            return "success";
        }
        return "fail";
    }

    @Override
    public String deleteFavorite(String userid, String vaccids) {
        String s = mapper.selectFavorite(userid);
        String[] split = s.split(";");
        StringBuilder sb = new StringBuilder("");
        for (String value : split) {
            if (!value.equals(vaccids)) {
                sb.append(value).append(";");
            }
        }
        Boolean flag = mapper.addFavorite(userid, sb.toString());
        if (flag) {
            return "success";
        }
        return "fail";
    }

    @Override
    public Boolean createOrder(VaccOrder vaccOrder) {
        vaccOrder.setOrderFinished(Short.parseShort("0"));
        Timestamp d = new Timestamp(System.currentTimeMillis());
        String s = vaccOrder.getUid() + "-" + d.toString().trim();
        s = s.replace(" ", "-").replace(":", "-").replace(".", "-");
        vaccOrder.setOrderId(s);
        return mapper.createOrder(vaccOrder);
    }

    @Override
    public Boolean finishOrder(String oid) {
        LocalDateTime time = LocalDateTime.now();
        return mapper.finishOrder(oid, time);
    }

    @Override
    public LinkedList<VaccOrder> selectOrder(String uid) {
        return mapper.selectOrder(uid);
    }


}
